package com.lineage.server.timecontroller.npc;

import com.lineage.server.model.Instance.L1DeInstance;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.world.WorldDe;
import java.util.Collection;
import java.util.Iterator;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class NpcShopTimer extends TimerTask {
  private static final Log _log = LogFactory.getLog(NpcShopTimer.class);
  
  private ScheduledFuture<?> _timer;
  
  public void start() {
    int timeMillis = 8000;
    this._timer = GeneralThreadPool.get().scheduleAtFixedRate(this, 8000L, 8000L);
  }
  
  public void run() {
    try {
      Collection<L1DeInstance> allDe = WorldDe.get().all();
      if (allDe.isEmpty())
        return; 
      Iterator<L1DeInstance> iter = allDe.iterator();
      while (iter.hasNext()) {
        L1DeInstance de = iter.next();
        if ((!de.destroyed() || !de.isDead()) && de.getCurrentHp() > 0) {
          if (de.isShop())
            de.shopChat(); 
          if (de.get_chat() != null)
            de.globalChat(); 
          Thread.sleep(50L);
        } 
      } 
    } catch (Exception e) {
      _log.error("Npc 虚拟商店时间轴异常重启", e);
      GeneralThreadPool.get().cancel(this._timer, false);
      NpcShopTimer shopTimer = new NpcShopTimer();
      shopTimer.start();
    } 
  }
}
